# list

# 创建create
# - + 组合
# tips: =+可以list =+ str 得到list,+则必须两个list
list_1 = ['8'] + ['3']
print(list_1)
list_1 += "5"
print(list_1)
# - * 重复
str_2 = '测试+'
list_2 = ['测试+'] * 2
print(f"{str_2}\n{list_2}")
print(id(str_2), id(list_2[0]))
list_2 *= 2
print(f"自乘后：{list_2}")
# - append添加：末尾添加
list_2.append('586')
print(f"append后：{list_2}")
# - insert添加：指定位置添加
list_2.insert(3, 54)
print(f"insert后：{list_2}")

# 检索Retrieve
# - 索引
print(f"[2]：{list_2[2]}")
# - 切片
list_slice = list_2[3:6:2]
list_slice2 = list_2[-1]
print(f"切片后：{list_slice},-1取最后一个值{list_slice2}")
# - index索引
print(f"index取586的索引值：{list_2.index('586')}")
list_2.insert(list_2.index('586'), ['abc', "a"])
print(f"在586前插入1个值：{list_2}")

# 更新update
# - 切片赋值
list_2[1] = "yz"
print(f"把[1]改为yz：{list_2}")
list_2[5] = "yyj"
print(f"把[5]改为yjj：{list_2}")

# 删除delete
# - pop删除最后一个并返回删除值
pop_1 = list_2.pop()
print(f"pop删除的内容为{pop_1}\n结果为：{list_2}")
# - remove指定删除找到的第一个值，返回None
remove_1 = list_2.remove("测试+")
print(f"remove删除的内容为{remove_1}\n结果为：{list_2}")

# 统计排序
# - 翻转
# - - reverse-改变原list
# - - reversed返回新list(返回反向迭代器),需要list(),tuple(),next()或者"".join()等
# - 排序
# - - sort改变原list
# - - sorted返回新list
print(f"list_2中有{list_2.count('测试+')}个测试+")
list_2.reverse()
print(f"reverse后{list_2}")
list_reversed = list(reversed(list_2))
print(f"reversed后{list_reversed}")
list_sorted = sorted(list_1)
print(f"sorted后list_1:{list_1}\n新list:{sorted(list_sorted)}")
list_1.sort()
print(f"sort后list_1{list_1}")

# tuple

# 创建create，tuple本意为重新赋值，而非其本身
tuple_1 = (1,)
print(id(tuple_1))
tuple_1 += (1, "2")
print(id(tuple_1))
tuple_1 *= 3
print(id(tuple_1))

# 检索Retrieve
# - 索引取值
print(f"[2]的值的类型为{type(tuple_1[2])}")
# - 切片
print(f"切片后为{tuple_1[2:6:1]}")
print(tuple_1)
# - index()返回索引，3为从[3]开始
print(tuple_1.index(1, len(tuple_1) - 1 - 1))

# dict

# 创建create
# - 键对值赋值
dict_1 = {'张三': 24}
print(dict_1)
# - update
dict_update = dict_1.update({"张四": 22, "张五": 23})
print(dict_1)
print(dict_update)
# - setdefault
dict_setdefault_1 = dict_1.setdefault("李四", 28)
dict_setdefault_2 = dict_1.setdefault("李四", 24)
print(dict_1)
print(dict_setdefault_2)

# 检索Retrieve
# - 通过键访问
print(f"李四的年龄是{dict_1['李四']}")
# - get
dict_1.get("张三", 1)
print(f"张三的年龄是{dict_1.get('张三', 1)}")
# - keys()、values()返回所有key或者值,items()返回tuple形式键对值
print(f"{dict_1.keys()}\n{dict_1.values()}\n{dict_1.items()}")

# 更新update(除了setdefault()和create基本一致)
# - 键对值赋值
dict_1['李四'] = 24
print(f"修改李四年龄为24：{dict_1}")
# - update
dict_update = dict_1.update({"李四": 25, "张五": 24})
print(f"修改李四、张五年龄：{dict_1}")

# 删除delete
# - pop指定删除
pop_2 = dict_1.pop("张五")
print(f"删除张五：{dict_1},删除的张五年龄为：{pop_2}")
# - popitem()随机删除元素，尽量不用(dict是无序的)
popitem_1 = dict_1.popitem()
print(f"随机删除：{dict_1},删除的内容为：{popitem_1}")
# - clear()
clear_1 = dict_1.clear()
print(f"全部清空：{dict_1},返回值：{clear_1}")

# set

# 创建create
# - add
set_1 = {1}
set_1.add(8)
set_1.add(9)
set_1.add(102)
print(set_1)
# 检索Retrieve
# - in:在list/tuple/dict的key/set中均可进行判断
print(1 in set_1)

# 更新update
# - update
set_1.update("a")
set_1.update({"b", "y"})
print(set_1)
# - union并集
set_2 = {"a", "c", "1"}
set_union_1 = set_1.union(set_2)
print(f"并集为：{set_union_1}")

# 删除delete
# - remove报错/discard不报错
set_1.remove("a")
set_1.discard("c")
print(f"删除后为：{set_1}")
# - pop随机删除
set_1.pop()
print(f"pop后为：{set_1}")
